package com.edou.member.controller.member;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.base.constant.SessionConstant;
import com.edou.base.controller.BaseController;
import com.edou.base.util.VerifyCode;
import com.edou.member.entity.MemberUser;
import com.edou.member.form.MemberUserForm;
import com.edou.member.service.IMemberUserService;


/**
 * 会员首页模块
 * @creator     admin
 * @create-time 2016/3/12 16:10:22
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Controller("memberUserRegistController")
@Scope("prototype")
@RequestMapping("/member")
public class MemberUserRegistController extends BaseController {

	static String servletUrl = "http://localhost:8080/";
	
	@Resource(name = "memberUserServiceImpl")
	private IMemberUserService memberUserService;
	
	/**
	 * 注册验证：帐号唯一性验证
	 * @return 
	 */
	@RequestMapping(value = "/check_account.htm", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView checkAccount(MemberUserForm model){
		String account = model.getAccount();
		MemberUser member = memberUserService.getMemberUserByAccount(account);
		if (null == member) {
			return ajaxJSON(Status.success,"true");
		} else {
			return ajaxJSON(Status.success,"false");
		}
	}
	
	/**
	 * 注册验证：邮箱唯一性验证
	 * @return 
	 */
	@RequestMapping(value = "/check_email.htm", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView checkEmail(MemberUserForm model){
		String email = model.getEmail();
		MemberUser member = memberUserService.getMemberUserByEmail(email);
		if (null == member) {
			return ajaxJSON(Status.success,"true");
		} else {
			return ajaxJSON(Status.success,"false");
		}
	}
	
	/**
	 * 注册验证：手机唯一性验证
	 * @return 
	 */
	@RequestMapping(value = "/check_mobile.htm", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView checkMobile(MemberUserForm model){
		String mobile = model.getMobile();
		MemberUser member = memberUserService.getMemberUserByMobile(mobile);
		if (null == member) {
			return ajaxJSON(Status.success,"true");
		} else {
			return ajaxJSON(Status.success,"false");
		}
	}
	
	/**
	 * 生产验证码
	 * @return 
	 */
	@RequestMapping(value = "/verification.htm", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView verification(MemberUserForm model,HttpSession session,HttpServletResponse response){
		//1.获取随机数
		String verifyCodeStr = VerifyCode.generateTextCode(VerifyCode.TYPE_NUM_ONLY, 4, null);
		logger.info("生成的验证码：" + verifyCodeStr);
		
		//3.发送短信
		String message = "验证码：" + verifyCodeStr + "，请勿向任何人泄漏此验证码。如非本人操作，请忽略此消息。";
		session.setAttribute(SessionConstant.SMSCODE, verifyCodeStr);
	
		return ajaxJSON(Status.success,verifyCodeStr);
	}
	
		
	
}